package com.jinyi.wwdeng.ui.main.impl

import android.content.Context
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.wwdeng.ui.main.contract.CreditApplyFailedContract
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/28 16:21
 * @Annotation：Presenter 实现类
 */
class CreditApplyFailedPresenterImpl: BasePresenter<CreditApplyFailedContract.View,CreditApplyFailedModelImpl>(),CreditApplyFailedContract.Presenter {

    override fun createModel(): CreditApplyFailedModelImpl {
        return CreditApplyFailedModelImpl(this)
    }

    /**
     * 获取推荐产品列表
     */
    fun loadRecommendAppListRequest(){
        if (isViewDestory()) {
            return
        }
        model!!.loadRecommendAppListRequest()
    }

    /**
     * 加载推荐产品列表成功
     */
    override fun onRecommendAppListSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        when(data.optInt("code")){
            0 -> {//成功
                val obj = data.optJSONObject("obj")
                val appList = arrayListOf<JSONObject>()
                if (obj.has("dcProductList")){
                    val appArray = obj.optJSONArray("dcProductList")
                    if (appArray != null && appArray.length() > 0){
                        for (i in 0..<appArray.length()){
                            appList.add(appArray.optJSONObject(i))
                        }
                    }
                }
                mvpView!!.showRecommendAppList(appList)
            }
            else -> {//失败
                mvpView!!.showToast(data.optString("msg"))
            }
        }
    }


    override fun onRequestFinish() {
        mvpView!!.onRequestFinish()
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}